package modelLayer;

/**
 * This class is part of WesternStyle sales management system. 
 * It holds information on a given orderLine of an order.
 * 
 * @author A. Kelemen (Design)
 * 		   ...	(Review)
 * 		   ...	(Test)
 * @Version 1.1
 * 			
 */
public class OrderLine {
	
	private int 	   amount;
	private Product    product = new Product();
	private SalesOrder salesOrder;
	
	/**
	 * Constructs an instance of OrderLine.
	 * 
	 * This constructor takes no parameters.
	 */
	public OrderLine() {

		
		
	}
	
	/**
	 * Constructs an instance of OrderLine.
	 * This constructor controls the values passed down from the user-interface. If any of the values doesn't comply with the
	 * standards set forth, an exception will be thrown, and afterwards caught in the uiLayer,
	 * describing the problem to the user.
	 * 
	 * @param amount	int		The amount of a given product associated to the orderLine.
	 * @param product	Product	A given product associated to the orderLine.
	 */
	public OrderLine(int amount, Product product) {
		
		if (!this.setAmount(amount)) {
			throw new IllegalArgumentException ("Antal varer skal være større end 0.");
		}
		if (!this.setProduct(product)) {
			throw new IllegalArgumentException("Der skal være tilføjet mindst en vare.");
		}
		
	}

	/**
	 * Returns the amount of a given product associated to the orderLine.
	 * 
	 * @return the amount
	 */
	public int getAmount() {
		
		return this.amount;
		
	}

	/**
	 * Sets the value of the amount.
	 * 
	 * @param amount the amount to set
	 */
	public boolean setAmount(int amount) {
		
		if (amount > 0) {
			this.amount = amount;
			return true;
		}
		else {
			return false;	
		}
		
	}

	/**
	 * Returns the given product associated to the orderLine.
	 * 
	 * @return the product
	 */
	public Product getProduct() {
		
		return this.product;
		
	}

	/**
	 * Sets the value of the given product associated to the orderLine.
	 * 
	 * @param product the product to set
	 */
	public boolean setProduct(Product product) {
		
		if (product != null) {
			this.product = product;
			return true;
		}
		else {
			return false;
		}
		
	}

	/**
	 * @return
	 */
	public SalesOrder getSalesOrder() {
		
		return this.salesOrder;
		
	}
	
	/**
	 * This method should only be called from salesOrder.
	 * 
	 * @param salesOrder
	 */
	protected void setSalesOrder(SalesOrder salesOrder) {
		
		this.salesOrder = salesOrder;

	}

	
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "OrderLine [amount=" + amount 
				+ ", product=" + (product == null ? "?" : product.getProductID())+ ", salesOrder=" + (salesOrder == null ? "?" : salesOrder.getOrderID()) + "]";
	}

	
}
